﻿using Abp.Application.Services.Dto;
using Abp.Authorization.Users;
using Abp.Domain.Repositories;
using Abp.Linq.Extensions;
using Abp.Organizations;
using Camc.Quality.DataDictionary;
using Camc.Quality.IOperationHistory;
using Camc.Quality.Materials.Dto;
using Camc.Quality.OperationHistory;
using Camc.Quality.UserSkillMartix;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Camc.Quality.UserSkillMatrix
{
    public class UserSkillAppService : QualityAppServiceBase
    {
        private readonly IRepository<OrganizationUnit, long> _organizationUnitRepository;
        private readonly IRepository<UserOrganizationUnit, long> _userOrganizationUnitRepository;
        private readonly IRepository<Sys_Code, Guid> _sysCodeRepository;
        private readonly IRepository<UserSkill, Guid> _userSkillRepository;
        private readonly IOperationHistoryAppService _iOperationHistoryAppService;
        public UserSkillAppService(IRepository<OrganizationUnit, long> organizationUnitRepository,
                                         IRepository<UserOrganizationUnit, long> userOrganizationUnitRepository,
                                         IRepository<Sys_Code, Guid> sysCodeRepository,
                                         IRepository<UserSkill, Guid> userSkillRepository,
                                         IOperationHistoryAppService iOperationHistoryAppService)
        {
            _organizationUnitRepository = organizationUnitRepository;
            _userOrganizationUnitRepository = userOrganizationUnitRepository;
            _sysCodeRepository = sysCodeRepository;
            _userSkillRepository = userSkillRepository;
            _iOperationHistoryAppService = iOperationHistoryAppService;
        }
        /// <summary>
        /// 获取技能列表
        /// </summary>
        /// <returns></returns>
        public async Task<List<MaterialsOtherDto>> GetSkillList()
        {
            var queryCode = from sc in _sysCodeRepository.GetAll().Where(s => s.IsUsed == 1)
                            join scc in _sysCodeRepository.GetAll()
                            on sc.PPtr equals scc.Id
                            where scc.Description == "人员技能"
                            select new MaterialsOtherDto
                            {
                                label = sc.Description,
                                value = sc.Id
                            };
            return queryCode.ToList();
        }
        /// <summary>
        /// 人员技能主表查询
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<UserSkillDto>> GetUserSkillList(GetUserSkillInput input)
        {
            var userSkillList = _userSkillRepository.GetAll();
            if (input.OrganizationUnitId != null)
            {
                userSkillList = userSkillList.Where(d => d.OrganizationUnitId == input.OrganizationUnitId);
            }
            if (input.UserId != null)
            {
                userSkillList = userSkillList.Where(d => d.UserId == input.UserId);
            }
            var count = userSkillList.Count();
            userSkillList = userSkillList.OrderByDescending(e => e.CreationTime).PageBy(input);
            var result = ObjectMapper.Map<List<UserSkillDto>>(userSkillList);
            return new PagedResultDto<UserSkillDto>(count, result);
        }
        /// <summary>
        /// 新增人员技能矩阵
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task CreateUserSkill(List<UserSkillDto> input)
        {
            foreach (var item in input)
            {
                var userSkill = new UserSkill()
                {
                    Id = Guid.NewGuid(),
                    OrganizationUnitId = item.OrganizationUnitId,
                    OrganizationUnit = item.OrganizationUnit,
                    UserId = item.UserId,
                    User = item.User,
                    SkillId = item.SkillId,
                    Skill = item.Skill,
                    IsUsed = (Bop.IsUsed)item.IsUsed
                };
                _iOperationHistoryAppService.InsertOperationLogNew(userSkill.Id, BusinessTypes.人员技能, OperationTypes.新增人员, "新增人员" + userSkill.User + ",添加技能" + userSkill.Skill);
                await _userSkillRepository.InsertAsync(userSkill);
            }
        }
        /// <summary>
        /// 修改人员技能矩阵
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task UpdateUserSkill(UserSkillDto input)
        {
            var userSkill = _userSkillRepository.FirstOrDefault(s=>s.Id == input.Id);
            userSkill.OrganizationUnitId = input.OrganizationUnitId;
            userSkill.OrganizationUnit = input.OrganizationUnit;
            userSkill.UserId = input.UserId;
            userSkill.User = input.User;
            userSkill.SkillId = input.SkillId;
            userSkill.Skill = input.Skill;
            userSkill.IsUsed = (Bop.IsUsed)input.IsUsed;
            _iOperationHistoryAppService.InsertOperationLogNew(userSkill.Id, BusinessTypes.人员技能, OperationTypes.修改技能, "修改技能" + userSkill.Skill);
            await _userSkillRepository.UpdateAsync(userSkill);
        }
    }
}
